<template>
	<view style="padding-top: 40upx;">
		<view class="inputArea" style="text-align: center;">
			<image class="logo-img" src="/static/images/money.png"></image>
		</view>
		<view class="text-xl padding text-center">
			<text class="text-black text-bold">记帐簿</text>
		</view>
		<view class="text-s padding text-center">
			<text class="text-gray">家常记账</text>
		</view>

		<view class="inputArea">
			<button class="login-button bg-gradual-orange" open-type="getUserInfo" @getuserinfo="goLogin">翻开记帐簿</button>
		</view>
	</view>
</template>

<script>
	import {
		callCloudFunction
	} from '@/common/public_util.js';

	let _self;
	export default {
		components: {},
		data() {
			return {

			}
		},
		onLoad() {
			_self = this;
		},
		methods: {
			goLogin() {
				_self.getUserInfo();
			},
			getUserInfo: () => {
				uni.showLoading({
					title: '正在授权登录中........'
				});
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							withCredentials: true,
							success: (infoRes) => {
								callCloudFunction('get_openid', {
									code: loginRes.code
								}, (res) => {
									uni.hideLoading();
									//获取到openid
									let openid = res.data.openid;
									let userInfo = {};
									console.log('infoRes', infoRes)
									userInfo.openid = openid;
									userInfo.avatarUrl =
										'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-ff74f3c7-adfb-4fad-9cad-19e2bbd68d65/f52f7ab0-8a4d-4036-8a5f-088ab5cd2cac.png';
									userInfo.nickName = '盈记用户';
									uni.setStorageSync('userInfo', userInfo);
									callCloudFunction('user_add', userInfo, (res) => {
											console.log('用户信息:res', res);
											// if (res.upsertedId == null) {
											// 	uni.navigateTo({
											// 		url: '/pages/user/AvatarFilling'
											// 	})
											// 	return
											// } else {
											uni.switchTab({
												url: '/pages/app/bookkeeping/bookkeeping'
											});
											// }
										},
										(err) => {
											console.log('用户信息:err', err);
											uni.switchTab({
												url: '/pages/app/bookkeeping/bookkeeping'
											});
										});
								});
							},
							fail: (fail) => {
								uni.hideLoading();
								uni.showToast({
									icon: "none",
									title: '翻开记数簿要允许授权'
								});
							}
						});
					},
					fail: (fail) => {
						uni.hideLoading();
						console.log(JSON.stringify(fail));
					}
				});
			}
		}
	}
</script>

<style>
	.inputArea {
		padding: 30upx 10%;
	}

	.inputClass {
		border: 2px solid #ccc;
		border-radius: 44upx;
		outline: 0;
		width: 90%;
		padding: 16upx 30upx;
		background-color: #FFFFFF;
	}

	.logo-img {
		width: 150upx;
		height: 150upx;
		border-radius: 150upx;
	}

	.login-button {
		/* background: #2fc25b; */
		color: #FFFFFF;
		border-radius: 54upx;
		font-size: 36upx;
		text-align: center;
		padding: 20upx;
	}
</style>
